<%--
  Created by IntelliJ IDEA.
  User: lichengming
  Date: 2017/8/22
  Time: 下午1:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/resources.jsp" %>
<html>
<head>
    <title>秒杀专区配置列表</title>
</head>
<body>

<div>
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">查询条件</h3>
        </div>
        <div class="panel-body">
            <form class="form-inline ems_search_form">
                <div class="form-group">
                    <input type="text" class="form-control" id="beginTime" name="beginTime" placeholder="起始创建时间">
                    <label>-</label>
                    <input type="text" class="form-control" id="endTime"  name="endTime" placeholder="截止创建时间">
                </div>
                <div class="form-group">
                    <label for="status">状态：</label>
                    <select id="status" name="status" class="form-control">
                        <option value = "" selected>请选择</option>
                        <option value ="0">停止</option>
                        <option value ="1">启用</option>
                    </select>
                </div>
                <div class="form-group">
                    <button type="button" class="btn btn-info btn-search">查询</button>
                    <button type="button" class="btn btn-default btn-reset">清空</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="ems_table_content">
    <div class="panel panel-default">
        <div class="panel-heading">秒杀专区配置列表</div>
        <div id="table_tools">
            <div class="form-inline" role="form">
                <button type="button" id="seckill_add" class="btn btn-outline btn-success">
                    <i class="glyphicon glyphicon-plus" aria-hidden="true"></i>
                    添加
                </button>
                <button type="button" id="seckill_edit" class="btn btn-outline btn-primary">
                    <i class="glyphicon glyphicon-edit" aria-hidden="true"></i>
                    修改
                </button>
                <button type="button" id="seckill_stop" class="btn btn-outline btn-danger">
                    <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                    停止
                </button>
                <button type="button" id="seckill_start" class="btn btn-outline btn-success">
                    <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                    启用
                </button>
                <button type="button" id="seckill_item" class="btn btn-outline btn-primary">
                    <i class="glyphicon glyphicon-list" aria-hidden="true"></i>
                    活动商品列表
                </button>
            </div>
        </div>
        <table id="seckillTable"
               data-url="/ems/seckill/pagination"
               data-show-export="true">
            <thead>
            <tr>
                <th data-field="state" data-checkbox="true"></th>
                <%--<th data-field="price">最低价格</th>--%>
                <th data-field="begin_time">开始时间</th>
                <th data-field="end_time">结束时间</th>
                <th data-field="create_time">创建时间</th>
                <th data-field="update_time">更新时间</th>
                <th data-field="status" data-formatter="statusFormat">状态</th>
            </tr>
            </thead>
        </table>
    </div>
</div>
<script src="/ems/js/distpicker.min.js"></script>
<script>

    function statusFormat(srcValue){

        if (srcValue == 1) {
            return "<span style='color: #3cb371;font-weight: bold' >进行中</span>";
        }else if(srcValue == 2){
            return "<span style='color: red;font-weight: bold'>未开始</span>";
        }else {
            return "<span style='color: #3a575e;font-weight: bold'>已结束</span>";
        }

    }

    $(function () {
        $("#seckillTable").createTable();

        $('#beginTime,#endTime').datetimepicker({
            language : 'zh-CN',
            format : 'yyyy-MM-dd HH:mm:ss',
        });

        //添加
        $("#seckill_add").on("click",function () {
            window.location.href = "/ems/seckill/edit"
        })

        //编辑
        $("#seckill_edit").on("click",function () {
            var selectRows =  $('#seckillTable').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要编辑的数据");
                return;
            }
            if (selectRows.length > 1) {
                $.errorAlert("暂时不支持多行数据编辑");
                return;
            }

            var status = selectRows[0].status;

            if( status == "3"){
                $.errorAlert("已结束活动不能编辑");
                return;
            }

            window.location.href = "/ems/seckill/edit?id=" + selectRows[0].id;
        })

        //商品列表
        $("#seckill_item").on("click",function () {
            var selectRows =  $('#seckillTable').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要编辑的数据");
                return;
            }
            if (selectRows.length > 1) {
                $.errorAlert("暂时不支持多行数据编辑");
                return;
            }
            window.location.href = "/ems/seckill/item?id=" + selectRows[0].id;
        })

        //停止
        $("#seckill_stop").on("click",function () {
            var selectRows =  $('#seckillTable').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要停止的活动");
                return;
            }

            var seckillIdList = "";
            for (var i = 0;i<selectRows.length;i++) {
                seckillIdList += selectRows[i].id + ",";
            }
            seckillIdList = seckillIdList.substring(0,seckillIdList.length - 1);
            $.confirmAlert("你确定要停止这些活动吗",function(){
                $.emsAjax({
                    data:{seckillIdList:seckillIdList},
                    type:"POST",
                    url:"/ems/seckill/stop",
                    success:function(resp) {
                        if (resp.status == "000") {
                            $.alert(resp.message);
                            search("#seckillTable",getQueryParams());
                        }else {
                            $.errorAlert(resp.message);
                        }
                    }
                });
            });
        });

        //启用
        $("#seckill_start").on("click",function () {
            var selectRows =  $('#seckillTable').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要停止的活动");
                return;
            }

            var seckillIdList = "";
            for (var i = 0;i<selectRows.length;i++) {
                seckillIdList += selectRows[i].id + ",";
            }
            seckillIdList = seckillIdList.substring(0,seckillIdList.length - 1);
            $.confirmAlert("你确定要启用这些活动吗",function(){
                $.emsAjax({
                    data:{seckillIdList:seckillIdList},
                    type:"POST",
                    url:"/ems/seckill/start",
                    success:function(resp) {
                        if (resp.status == "000") {
                            $.alert(resp.message);
                            search("#seckillTable",getQueryParams());
                        }else {
                            $.errorAlert(resp.message);
                        }
                    }
                });
            });
        });
    });
</script>
</body>
</html>
